<template>
	<view class="content">
		<music v-if="isplayingmusic" @to="toPlayer()" :img="curimg"></music>
		<view class="topbar">
			<view class="back" @tap="back()">
				<image src="../../static/image/search/back.png" mode=""></image>
			</view>
			<view class="searchbox">
				<view class="input">
					{{ keywords }}
				</view>
			</view>
			<view class="search">
				<image @tap="playAll()" src="../../static/image/search/play.png" mode=""></image>
			</view>
		</view>
		<view class="songlist">
			<view class="card" v-for="(item,index) in songslist" :key='index'>
				<view class="title">
					<view class="img">
						<view class="box">
							<image :src="item.picurl" mode=""></image>
						</view>
					</view>
					<view class="songname">
						{{item.songname}}
					</view>
					<view class="add" @tap="addOne(index)">
						<image src="../../static/image/search/add.png" mode=""></image>
					</view>
					<view class="play" @tap="playOne(index)">
						<image src="../../static/image/search/play.png" mode=""></image>
					</view>
				</view>
				<view class="msg">
					<view class="singer">
						{{item.artistname}}
					</view>
					<view class="time">
						{{item.time + ' min'}}
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import request from '../../utils/request.js'
	import {
		mapGetters,
		mapMutations
	} from 'vuex'
	import Vue from 'vue'
	export default {
		data() {
			return {
				keywords: '',
				songslist: [],
				isplaying: false,
			};
		},
		onLoad(e) {
			this.keywords = e.key;
			this.search(this.keywords)
		},
		computed: {
			...mapGetters(['curimg', 'curindex', 'audiolist','isplayingmusic']),
		},
		methods: {
			...mapMutations(['setCurImg', 'setCurIndex', 'setAudiolist', 'setIsplayingmusic']),
			//搜索歌曲
			search: function() {
				this.songslist = [];
				if (this.keywords) {
					let opts = {
						url: '/search',
						data: {
							keywords: this.keywords
						},
						method: 'GET',
					}
					request.request(opts).then((res) => {
						res.result.songs.forEach((item, index) => {
							let id = item.id;
							let songname = item.name;
							let artistname = item.artists[0].name;
							let time = (item.duration / 1000 / 60).toFixed(2);
							let picurl = item.artists[0].img1v1Url;
							// let opts = {
							// 	url:'/artists',
							// 	data:{
							// 		id: item.artists[0].id
							// 	},
							// 	method:'GET'
							// };
							// request.request(opts).then(res=>{
							// 	console.log(res)
							// 	// picurl = res.artists[0].picUrl;
							// });
							this.songslist.push({
								id,
								songname,
								artistname,
								time,
								picurl,
								url: '123',
								like:false
							});
						});
					});
					// console.log(this.songslist);
				} else {
					uni.showToast({
						icon: 'none',
						title: '内容不能为空！',
						duration: 2000,
					})
				}
				// console.log(this.songslist)
			},

			//跳转到主页
			back: function() {
				uni.navigateTo({
					url: '../index/index'
				})
			},
			
			//播放一首歌
			playOne:async function(index){
				let res = await this.geturl(this.songslist[index].id);
				this.setCurImg('../../static/image/test/icon.png')
				this.setIsplayingmusic(true)
				this.setCurIndex(0);
				this.songslist[index].url = res.data.data[0].url;
				if(this.isLike(this.songslist[index].id)){
					this.this.songslist[index].like = true;
				}
				if(res.data.data[0].url){
					this.setAudiolist([this.songslist[index]])
					Vue.prototype.$au_player.src = this.audiolist[this.curindex].url;
					Vue.prototype.$au_player.play();
				}else{
					uni.showToast({
						icon:'none',
						title:'歌曲跑丢了'
					})
				};
				
			},
			
			//添加一首歌
			addOne:async  function(index){
				if(this.isInList(this.songslist[index].id)){
					if (this.isplayingmusic) {
						let res = await this.geturl(this.songslist[index].id);
						this.setCurImg('../../static/image/test/icon.png')
						this.songslist[index].url = res.data.data[0].url;
						if(this.isLike(this.songslist[index].id)){
							this.this.songslist[index].like = true;
						}
						let list = this.audiolist;
						list.push(this.songslist[index]);
						this.setAudiolist(list);
						uni.showToast({
							icon:'none',
							title:'已加入播放列表'
						})
						// console.log(this.audiolist)
					}else{
						this.playOne(index);
					}
				}else{
					//
				}
			},

			//播放所有歌曲
			async playAll() {
				this.setCurIndex(0);
				this.setIsplayingmusic(true);
				this.setCurImg(this.songslist[this.curindex].picurl);
				this.playOne(0);
				this.isplaying = true;
				let result;
				for(let i=0;i<this.songslist.length;i++){
					result = await this.geturl(this.songslist[i].id);
					this.songslist[i].url = result.data.data[0].url;
					if(this.isLike(this.songslist[i].id)){
						this.this.songslist[i].like = true;
					}
				};
				this.setAudiolist(this.songslist)
				// let url = this.songslist[0].url;
				// console.log(this.audiolist)
				// console.log((this.songslist[0]).url);
				// Vue.prototype.$au_player.src = url;
				// Vue.prototype.$au_player.play();
				// console.log(url);
			},

			//跳转到播放页面
			toPlayer:function() {
				uni.navigateTo({
					url: "../player/player"
				})
			},

			//获取url
			geturl: function(id) {
				return new Promise((resolve, reject) => {
					uni.request({
						url: "https://qqyifeng.cn:9001/song/url",
						data: {
							id: id,
						},
						method: "GET",
						success:function(res){
							// console.log(res)
							resolve(res);
						}
					});
				})
			},
			
			//是否在列表里面
			isInList: function(id){
				for(let i=0;i<this.audiolist.length;i++){
					if(id == this.audiolist[i].id){
						uni.showToast({
							icon:'none',
							title:'歌曲已经在列表里面啦~'
						})
						return false;
					}
				}
				return true;
			},
			
			//首先查看此歌是否位于我的歌单列表
			isLike: function(){
				try {
				    this.likelist = uni.getStorageSync('mylist');
				} catch (e) {
				    uni.showToast({
				    	icon:"none",
						title:"获取我的歌单失败"
				    })
				}
				for(let i=0;i<this.likelist.length;i++){
					if(this.audiolist[this.curindex].id == this.like[i].id){
						return 1;
					}
				}
				return 0;
			},

		}
	}
</script>

<style lang="scss">
	.content {
		width: 100%;
		height: 100%;
		box-sizing: border-box;
		padding: $uni-spacing-row-sm 10rpx;
		// padding-top: var(--status-bar-height);
		// bottom: env(safe-area-inset-bottom);

		.topbar {
			display: flex;
			width: 100%;
			height: 72rpx;
			padding-bottom: 20rpx;
			border-bottom: 1rpx solid #F1F1F1;

			image {
				width: 60rpx;
				height: 60rpx;
			}

			.back {
				felx: 1;
				display: flex;
				justify-content: center;
				align-items: center;
				text-align: center;
			}

			.searchbox {
				flex: 7;
				color: #666;
				margin: 0 8rpx;

				.input {
					width: 100%;
					height: 72rpx;
					line-height: 72rpx;
					text-align: center;
					color: #666;
					border-radius: 12rpx;
				}
			}

			.search {
				flex: 1;
				display: flex;
				justify-content: center;
				align-items: center;
				text-align: center;
			}
		}

		.songlist {
			width: 100%;
			padding: $uni-spacing-row-base;
			box-sizing: border-box;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;

			.card {
				position: relative;
				width: 95%;
				height: 160rpx;
				margin-bottom: 50rpx;
				border-radius: 24rpx;
				padding: 20rpx 20rpx 0rpx 20rpx;
				box-shadow: 10rpx 10rpx 20rpx #808080;
				overflow: hidden;

				.title {
					display: flex;

					.img {
						flex: 2;

						.box {
							margin-left: 20rpx;
							width: 130rpx;
							height: 130rpx;
							border-radius: 12rpx;
							overflow: hidden;
							box-shadow: 1rpx 1rpx 20rpx #C0C0C0;

							image {
								width: 130rpx;
								height: 130rpx;
							}
						}
					}

					.songname {
						padding-left: 20rpx;
						flex: 4;
						line-height: 100rpx;
						font-size: 40rpx;
						font-weight: 550;
						color: #666;
						display: -webkit-box !important;
						text-overflow: ellipsis;
						overflow: hidden;
						-webkit-line-clamp: 1;
						-webkit-box-orient: vertical;
					}

					.add {
						flex: 1;
						padding-top: 24rpx;

						image {
							width: 60rpx;
							height: 60rpx;
						}
					}

					.play {
						flex: 1;
						padding-top: 24rpx;

						image {
							width: 60rpx;
							height: 60rpx;
						}
					}
				}

				.msg {
					position: relative;
					padding-top: -20rpx;

					.singer {
						position: absolute;
						top: -36rpx;
						margin-left: 180rpx;
						font-size: 32rpx;
						color: #808080;
						display: -webkit-box !important;
						text-overflow: ellipsis;
						overflow: hidden;
						-webkit-line-clamp: 1;
						-webkit-box-orient: vertical;
					}

					.time {
						position: absolute;
						top: -36rpx;
						right: 38rpx;
						font-size: 32rpx;
						color: #808080;
						display: -webkit-box !important;
						text-overflow: ellipsis;
						overflow: hidden;
						-webkit-line-clamp: 1;
						-webkit-box-orient: vertical;
					}
				}
			}
		}
	}
</style>
